/**
 * 常用JS变量:
 * agentEvent = 代理模式下自动点击模块
 * acEvent= 无障碍模式下自动点击模块
 * device = 设备信息模块
 * file = 文件处理模块
 * http = HTTP网络请求模块
 * shell = shell命令模块
 * thread= 多线程模块
 * image = 图色查找模块
 * utils= 工具类模块
 * global = 全局快捷方式模块
 * 常用java变量：
 *  context : Android的Context对象
 *  javaLoader : java的类加载器对象
 * 导入Java类或者包：
 *  importClass(类名) = 导入java类
 *      例如: importClass(java.io.File) 导入java的 File 类
 *  importPackage(包名) =导入java包名下的所有类
 *      例如: importPackage(java.util) 导入java.util下的类
 *
 */

function main() {
//   utils.openAppByName("i讯飞");
//   sleep(3000);
//   let yy = text("应用");
//   let result = click(yy);
while(true) {
let reLocal = text("重新定位");
if (reLocal) {
let clkRelocal = click(reLocal);
if (clkRelocal) {
logd("点击重新定位成功");
sleep(5000);
} else {
 loge("重新定位失败");
 }
} else {
logi("定位正常，无需重新定位");
}
let timeStr = timeFormat("yyyy-MM-dd HH:mm");
logd(timeFormat("yyyy-MM-dd HH:mm:ss"));

let startTime = "2024-12-26";
let endTime = "2024-12-27";
if (timeStr == startTime + " 20:40" || timeStr == startTime + " 20:43" ||
timeStr == endTime + " 01:24" || timeStr == endTime + " 01:25") {
    let randomSleep = 0//random(30000,35000);
    logi("==========进入定时等待 " + randomSleep + "秒==========");
    sleep(randomSleep);
} else {
    logi("==========时间未到，10秒后重试==========");
    sleep(10000);
    continue;
}
logd(timeFormat("yyyy-MM-dd HH:mm:ss"));
let call = text("打卡");
let clickCall = click(call);
sleep(3000);
let say = id("com.iflytek.iflyapp:id/attendance_voice_iv_voice");
let clickSay = longClick(say);
logi("长点击：" + clickSay);
let pwd = id("com.iflytek.iflyapp:id/attendance_pwd_et_pwd");
var result = waitExistNode(pwd, 10000);
  if (result) {
    toast("存在节点");
    inputText(pwd, "q1w2e3asd.");
    sleep(2000);
    click(text("打卡"));
    sleep(2000);
    back();
    sleep(2000);
    click(text("考勤"));
  }
}

    //开始再这里编写代码了！！
//    toast("Hello World");
//    var name = readConfigString("name");
//    logd("姓名: " + name);
//    logd("年龄: " + readConfigString("age"));
//    logd("听音乐: " + readConfigString("music"));
//    logd("是不是一年级: " + readConfigString("one"));
//    logd("备注: " + readConfigString("mark"));
//    logd("开始 " + isServiceOk());

    //如果自动化服务正常
    if (!autoServiceStart(3)) {
        logd("自动化服务启动失败，无法执行脚本")
        exit();
        return;
    }
    logd("开始执行脚本...")
//    if (!netcardProcessor()) {
//        return;
//    }
//    home();
}


function netcardProcessor() {
    logd("开始进行卡密验证")
    // 官方自带的卡密系统
    // appId 和 appSecret的值 请到 http://uc.ieasyclick.com/ 进行注册后提卡
    let appId = "";
    let appSecret = "";
    let cardNo = readConfigString("cardNo")
    if (cardNo == null || cardNo == undefined || cardNo.length <= 0) {
        toast("请输入卡密")
        loge("请输入卡密")
        exit()
        return false;
    }
    let inited = ecNetCard.netCardInit(appId, appSecret)
    logd("inited card => " + JSON.stringify(inited));
    let bind = ecNetCard.netCardBind(cardNo)
    let bindResult = false;
    if (bind != null && bind != undefined && bind["code"] == 0) {
        logd("卡密绑定成功")
        let leftDays = bind['data']['leftDays'] + "天";
        logd("剩余时间：" + leftDays);
        logd("激活时间：" + bind['data']['startTime'])
        logd("过期时间：" + bind['data']['expireTime'])
        bindResult = true;
        toast("卡密剩余时间:" + leftDays)
    } else {
        if (bind == null || bind == undefined) {
            loge("卡密绑定失败,无返回值 ")
            let msg = "卡密绑定失败,无返回值"
            loge(msg)
            toast(msg)
        } else {
            let msg = "卡密绑定失败: " + bind["msg"]
            loge(msg)
            toast(msg)
        }
    }
    return bindResult;
}


function autoServiceStart(time) {
    for (var i = 0; i < time; i++) {
        if (isServiceOk()) {
            return true;
        }
        var started = startEnv();
        logd("第" + (i + 1) + "次启动服务结果: " + started);
        if (isServiceOk()) {
            return true;
        }
    }
    return isServiceOk();
}

main();
